<template>
  <div class="login"
       :style="{backgroundImage: `url(${showLogo})`, backgroundSize: 'cover', backgroundRepeat: 'no-repeat'}"
  >
    <!-- 操作按钮 -->
<!--    <div class="login-opt pointer">
      <div class="fl">
        <el-tag size="mini" effect="dark" class="pointer" @click="goToRegister">注册</el-tag>
        <el-tag class="ml5" size="mini" effect="dark" @click="goToFindPassword">找回密码</el-tag>
      </div>
      <div class="fr">
        <el-link type="primary" class="color-white" @click="jumpGuide" :underline="false">新手指引</el-link>
        <span class="ml10 color-white">客服电话 13933322222</span>
      </div>
    </div>-->
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-width="70px"
             label-position="left"
    >
      <h3 class="title">{{ GET_XITONG.bossName }}</h3>
      <el-form-item prop="userName" label="用户名">
        <el-input
          v-model="loginForm.userName"
          type="text"
          size="small"
          style="width:250px"
          auto-complete="new-password"
          placeholder="用户名"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="small"
          style="width:250px"
          placeholder="密码"
          auto-complete="new-password"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled" label="验证码">
        <el-input
          size="small"
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width:80px"
          @keyup.enter.native="handleLogin"
        >
        </el-input>
        <div class="login-code ml10" style="width:120px">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <div class="pl20 pr10">
        <!-- <el-checkbox v-model="loginForm.autoLogin">两周内自动登录</el-checkbox> -->
        <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
        <el-form-item label="" label-width="0">
          <el-button
            :loading="loading"
            size="small"
            type="primary"
            style="width:100%;"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import Vue from 'vue'
import { mapGetters } from 'vuex'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        userName: '',
        password: '',
        rememberMe: false,
        autoLogin: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        userName: [
          { required: true, trigger: 'blur', message: '请输入用户名' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  computed: {
    ...mapGetters(['GET_XITONG']),

    showLogo() {
      return this.GET_XITONG.logiPcUrl.url.indexOf('?') > -1 ? this.GET_XITONG.logiPcUrl.url.slice(0, this.GET_XITONG.logiPcUrl.url.indexOf('?')) : this.GET_XITONG.logiPcUrl.url
    }
  },
  created() {
    this.getCode()
    this.getCookie()
    // this.autoTokenLogin()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.data.img
          this.loginForm.uuid = res.data.uuid
        }
      })
    },
    getCookie() {
      const userName = Cookies.get('userName')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        userName: userName === undefined ? this.loginForm.userName : userName,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('userName', this.loginForm.userName, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('userName')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch('Login', this.loginForm).then((res) => {
            // if (this.loginForm.autoLogin) {
            //   Vue.ls.set('Boss-Token',res.data.accessToken, 14 * 24 * 60 * 60 * 1000)
            // }
            this.$router.push({ path: this.redirect || '/' }).catch(() => {
            })
          }).catch(() => {
            if (this.captchaEnabled) {
              this.getCode()
            }
          }).finally(() => {
            setTimeout(() => {
              this.loading = false
            }, 1000)
          })
        }
      })
    },
    goToRegister() {
      this.$router.push('/register')
    },
    goToFindPassword() {
      this.$router.push('/findPassword')
    },
    autoTokenLogin() {
      const AdminToken = Vue.ls.get('Boss-Token')
      if (AdminToken) {
        // 调后端接口查询，检验token状态
        // 成功登录首页，失败清除Boss-Token返回首页
      }
    },
    jumpGuide() {
      window.open('/guide')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
}

.login-opt {
  position: absolute;
  left: 5px;
  right: 5px;
  top: 8px;
}

.login-form {
  background: #ffffff;
  padding: 25px 25px 5px 25px;

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  display: inline-block;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 32px;
}
</style>
